package game.source.gui.ship;

import game.source.gui.Animation;
import game.source.gui.Explosion;
import game.source.gui.RespawnFilter;
import game.source.ships.GuiPlayerShipIterface;
import game.source.util.Time;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
/**
 * GuiShip:
 * 	Classe que trata a parte gráfica da nave do player: 
 * 	Basicamente gerencia as animações na tela
 * */
public class GuiPlayerShip extends GuiShip implements GuiPlayerShipIterface{
	private static Animation anim;	//As imagens do avião do player
	private int frame;
	private static int height;		//altura da imagem
	private static int width;		//largura da imagem
	private boolean	respawn;
	private long t1;
	private long respawnTime;
	private	Time	time;
	
	/*Constantes de animação: Indicam em que frame começa e termina cada animação*/
	private final int TURN_LEFT_START 	= 13;
	private final int TURN_LEFT_END 	= 24;
	private final int TURN_RIGHT_START 	= 1;
	private final int TURN_RIGHT_END 	= 12;
	private final int EXPLODE_START		= 25;
	private final int EXPLODE_END		= EXPLODE_START + Explosion.getNumFrames() -1;
	
	static {
		BufferedImage img = null;
		GuiPlayerShip.anim = new Animation();
		for(int c = 0; c < 25 ;c++){
			try{
				img = ImageIO.read(new File(GuiPlayerShip.class.getResource("/images/player/"+c+".png").getFile()));
			}catch (Exception e) {
				e.printStackTrace();
				System.err.println("Arquivo: "+GuiPlayerShip.class.getResource("/images/player/"+c+".png")+" não pode ser aberto");
				System.exit(1);
			}
			GuiPlayerShip.anim.add(img);
		}
		GuiPlayerShip.height = img.getHeight(null);
		GuiPlayerShip.width = img.getWidth(null);
		
		for(int i = 0;i < Explosion.getNumFrames() ; i++){
			GuiPlayerShip.anim.add(Explosion.getFrame(i));
		}
		
	}
	
	public GuiPlayerShip(){
		this.frame = 0;
		this.respawn = false;
		this.t1 = 0;
		this.time	= new Time();
	}
	
	@Override
	public void respawn(long time){
		this.respawnTime = time;
		this.respawn = true;
	}
	
	@Override
	public void show(long time){
		if(this.respawn){
			if(t1 == 0){
				t1 = this.time.getTime();
			}else{
				if(this.time.getTime()-t1 >= this.respawnTime){
					t1 = 0;
					this.respawn = false;
				}
			}
			int tmp = this.frame;
			RespawnFilter.filter(GuiPlayerShip.anim.getFrame(tmp),GuiPlayerShip.anim.getFrame(tmp));
			super.show(time);
			RespawnFilter.returnsPrevius(GuiPlayerShip.anim.getFrame(tmp));
		}else{
			super.show(time);
		}
	}
	
	@Override
	public Animation getAnimation(){
		return GuiPlayerShip.anim;
	}
	@Override
	public int getWidth() {
		return GuiPlayerShip.width;
	}
	@Override
	public int getHeight() {
		return GuiPlayerShip.height;
	}
	@Override
	public int getFrame() {
		return this.frame;
	}
	@Override
	public void setFrame(int frame) {
		this.frame = frame;
	}
	@Override
	public int getTurnLeftStart() {
		return this.TURN_LEFT_START;
	}
	@Override
	public int getTurnLeftEnd() {
		return this.TURN_LEFT_END;
	}
	@Override
	public int getTurnRightStart() {
		return this.TURN_RIGHT_START;
	}
	@Override
	public int getTurnRightEnd() {
		return this.TURN_RIGHT_END;
	}

	@Override
	public int getExplosionStart() {
		return this.EXPLODE_START;
	}

	@Override
	public int getExplosionEnd() {
		return this.EXPLODE_END;
	}

	@Override
	public void play() {
		super.play();
		this.time.play();
	}

	@Override
	public void pause() {
		super.pause();
		this.time.pause();
	}
	
	
}
